package com.asset.dispoal.business.mediation.service.common;

import com.asset.dispoal.business.bsCase.domain.db.entity.BsCase;
import com.asset.dispoal.business.bsCase.domain.db.entity.BsCaseDisposalFile;
import com.asset.dispoal.business.bsCase.domain.db.entity.CaseStateRecord;
import com.asset.dispoal.business.bsCase.domain.db.mapper.BsCaseMapper;
import com.asset.dispoal.business.mediation.dto.SaveReduceInfoRequest;
import com.asset.dispoal.commom.menus.AgreementCaseSateMenu;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import tech.mhuang.pacebox.core.date.DateTimeUtil;
import tech.mhuang.pacebox.core.id.BaseIdeable;
import tech.mhuang.pacebox.springboot.core.local.GlobalHeaderThreadLocal;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDateTime;

/**
 * @description:
 * @author: yf
 * @date: 2022-09-15 11:05:42
 */
@Component
public class SaveReconcileFileCommand {
    @Autowired
    Environment environment;

    @Autowired
    BaseIdeable<String> ideable;

    @Autowired
    BsCaseMapper caseMapper;

    @SneakyThrows
    @Transactional
    public boolean execute(SaveReduceInfoRequest request, String fileType) {
        String userId = GlobalHeaderThreadLocal.getOrException().getUserId();
        String headUrl = environment.getProperty("file.evidence.url");
        String caseRegisterFolder = environment.getProperty("file.caseRegister.url");
        LocalDateTime currentLocalDate = LocalDateTime.now();
        Path relativePath = Paths.get(caseRegisterFolder,
                String.valueOf(currentLocalDate.getYear()),
                String.valueOf(currentLocalDate.getMonthValue()),
                String.valueOf(currentLocalDate.getDayOfMonth()),
                fileType
        );

        Path checkDirFile = Paths.get(headUrl, relativePath.toString());
        if (Files.notExists(checkDirFile)) {
            Files.createDirectories(checkDirFile);
        }
        for (MultipartFile file : request.getReconcileFiles()) {
            String originalName = file.getOriginalFilename();
            String replaceName = ideable.generateId() + originalName;
            file.transferTo(Paths.get(checkDirFile.toString(), replaceName));
            BsCaseDisposalFile caseDisposalFile = new BsCaseDisposalFile();
            caseDisposalFile.setCaseId(request.getCaseId());
            caseDisposalFile.setId(ideable.generateId());
            caseDisposalFile.setReplaceName(replaceName);
            caseDisposalFile.setOriginalName(originalName);
            caseDisposalFile.setFilePath(relativePath + File.separator + replaceName);
            caseDisposalFile.setFileType(fileType);
            caseDisposalFile.setCurrentState(AgreementCaseSateMenu.RECONCILIATION.getLabel());
            caseDisposalFile.setCreateUser(userId);
            caseDisposalFile.setOperateUser(userId);
            caseDisposalFile.setCreateTime(DateTimeUtil.localDateTimeToDate(currentLocalDate));
            caseDisposalFile.setOperateTime(DateTimeUtil.localDateTimeToDate(currentLocalDate));
            caseDisposalFile.setUploadWay("0");
            caseMapper.insertDisposeFile(caseDisposalFile);
        }

        BsCase oldCase = caseMapper.getCaseById(request.getCaseId());
        //更改主表状态
        caseMapper.updateCaseState(request.getCaseId(), AgreementCaseSateMenu.RECONCILIATION.getLabel(), "1", userId);

        //插入记录表
        CaseStateRecord caseStateRecord = new CaseStateRecord();
        caseStateRecord.setId(ideable.generateId());
        caseStateRecord.setCaseId(request.getCaseId());
        caseStateRecord.setCreateUser(userId);
        caseStateRecord.setCreateTime(DateTimeUtil.currentDate());
        caseStateRecord.setOldState(oldCase.getCaseState());
        caseStateRecord.setNewState(AgreementCaseSateMenu.RECONCILIATION.getLabel());
        caseMapper.insertStateRecord(caseStateRecord);
        return true;
    }
}
